//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    workshopRecommend:{},
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  
  //分享
  onShareAppMessage: function (options) {
    return {
      title: this.data.userInfo.name+'的名片',
      imageUrl: this.data.userInfo.avatar,
      path: 'pages/index/index?user_id='+this.data.userInfo.user_id
    }

    },
  onLoad: function (options) {
    if(app.globalData.userInfo&&app.globalData.userInfo!=null){
      //获取用户信息
      this.setData({
        userInfo: app.globalData.userInfo
      })
      wx.setNavigationBarTitle({
        title: app.globalData.userInfo.name+'的名片',
      })
    }else{
       // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.emptyUserInfoCallback=userInfo=>{
        if (userInfo != null) {
          this.setData({
            userInfo: app.globalData.userInfo
          });
          wx.setNavigationBarTitle({
            title: app.globalData.userInfo.name + '的名片',
          })
        }
      
      }
    }
   
    //推荐房源请求
    wx.request({
      url: app.globalData.apiurl + "workshop_recommend",
    /*  header: {
        "Authorization": "bearer " + app.globalData.access_token
      },*/
      
      method: 'get',
      success: res=> {
        if(res.data.code==0){
            this.setData({
              workshopRecommend:res.data.data
            })
        }
      },
      fail: function () {
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  //地图
  callMap(){
          wx.openLocation({//​使用微信内置地图查看位置。
            latitude: parseInt(app.globalData.userInfo.lat),//要去的纬度-地址
            longitude: parseInt(app.globalData.userInfo.lng),//要去的经度-地址
            name: app.globalData.userInfo.address,
            address: app.globalData.userInfo.address
          })  
  },
  //电话
  makeCall: function(e){
    console.log(this.data.userInfo.mobile)
    wx.makePhoneCall({
      phoneNumber: this.data.userInfo.mobile
    })
  },
  //存入通讯录
  addPhone:function(e){
    wx.addPhoneContact({
      photoFilePath: '',
      nickName: '',
      lastName: '',
      middleName: '',
      firstName: this.data.userInfo.name,
      remark: '',
      mobilePhoneNumber: this.data.userInfo.mobile,
      weChatNumber: '',
      addressCountry: '',
      addressState: '',
      addressCity: '',
      addressStreet: '',
      addressPostalCode: '',
      organization: '',
      title: '',
      workFaxNumber: '',
      workPhoneNumber: '',
      hostNumber: '',
      email: '',
      url: '',
      workAddressCountry: '',
      workAddressState: '',
      workAddressCity: '',
      workAddressStreet: '',
      workAddressPostalCode: '',
      homeFaxNumber: '',
      homePhoneNumber: '',
      homeAddressCountry: '',
      homeAddressState: '',
      homeAddressCity: '',
      homeAddressStreet: '',
      homeAddressPostalCode: '',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    }) 
  }
  
})
